package com.pf.java.File;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * RandomAccessFile (随机访问文件)可控
 *
 * ClassName:IO9
 * Package:com.pf.java.File
 *
 * @author: pf
 * @date: 2021/11/8-9:11
 */
public class IO9 {

    public static void main(String[] args) {
        try (
                RandomAccessFile raf =
                        new RandomAccessFile("C:/Users/10970/Desktop/Java学习笔记/牛客网学习/out.txt", "rw");
                ){
            // 定位到末尾
            raf.seek(raf.length());
            raf.write("龙宝是王太龙\n".getBytes());
            raf.write("龙宝是王太龙\n".getBytes());
            // 定位至开头
            raf.seek(0);
            String line = null;
            while ((line = raf.readLine()) != null) {
                // String -> bytes (utf-8)
                // bytes -> String (ISO8859-1) 底层
                // String -> bytes (ISO8859-1)
                // bytes -> String (UTF-8)
                byte[] bytes = line.getBytes("ISO8859-1");
                line = new String(bytes, "UTF-8");
                System.out.println(line);
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
